
<!DOCTYPE html>
<html>
  <head>
    
<meta charset="utf-8" >

<title>C++笔记（1）——  数位分离 | 吴凡的个人博客</title>
<meta name="description" content="温故而知新">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://windowsregedit.github.io/favicon.ico?v=1644843428845">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link rel="stylesheet" href="https://windowsregedit.github.io/styles/main.css">



<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>



  </head>
  <body>
    <div id="app" class="main">
      <div class="site-header-container">
  <div class="site-header">
    <div class="left">
      <a href="https://windowsregedit.github.io">
        <img class="avatar" src="https://windowsregedit.github.io/images/avatar.png?v=1644843428845" alt="" width="32px" height="32px">
      </a>
      <a href="https://windowsregedit.github.io">
        <h1 class="site-title">吴凡的个人博客</h1>
      </a>
    </div>
    <div class="right">
      <transition name="fade">
        <i class="icon" :class="{ 'icon-close-outline': menuVisible, 'icon-menu-outline': !menuVisible }" @click="menuVisible = !menuVisible"></i>
      </transition>
    </div>
  </div>
</div>

<transition name="fade">
  <div class="menu-container" style="display: none;" v-show="menuVisible">
    <div class="menu-list">
      
        
          <a href="/" class="menu purple-link">
            首页
          </a>
        
      
        
          <a href="/archives" class="menu purple-link">
            归档
          </a>
        
      
        
          <a href="/tags" class="menu purple-link">
            标签
          </a>
        
      
        
          <a href="/post/about" class="menu purple-link">
            关于
          </a>
        
      
    </div>
  </div>
</transition>


      <div class="content-container">
        <div class="post-detail">
          
          <h2 class="post-title">C++笔记（1）——  数位分离</h2>
          <div class="post-info post-detail-info">
            <span><i class="icon-calendar-outline"></i> 2022-02-14</span>
            
              <span>
                <i class="icon-pricetags-outline"></i>
                
                  <a href="https://windowsregedit.github.io/tag/cGRbpm6Zr/">
                    C++笔记
                    
                  </a>
                
              </span>
            
          </div>
          <div class="post-content">
            <pre><code>   **int x = 142857;**
</code></pre>
<p>(取) 后一位 <strong>x % 10</strong>       (7)<br>
后两位** x % 100**     (57)<br>
后三位 <strong>x % 1000</strong>   (857)<br>
后四位 <strong>x % 10000</strong> (2857)</p>
<pre><code>   **(同上)**
</code></pre>
<p>(删除) 最后1位 <strong>x /= 10;</strong>         (14285)<br>
2位 <strong>x /= 100;</strong>       (1428)<br>
3位 <strong>x /= 1000;</strong>     (142)<br>
4位 <strong>x /= 10000;</strong>   (14)</p>
<pre><code>   ** (同上同上)**
(取出)  个位数字            **x % 10**    (7)
           十位            **x / 10 % 10**   (5)
           百位          **x / 100 % 10**   (8)
           千位        **x / 1000 % 10**   (2)
</code></pre>
<p>程序1：<br>
int x = 142857;<br>
x = x * 10 + 8;    -&gt;    1428578<br>
心得：<strong>讲一个数乘上10加上a（在1-10之间），相当于在末位上加上a。</strong></p>
<p>程序2：<br>
int x = 142857;<br>
x = x * 10    ( + 0);<br>
x = x * 10 + 1;<br>
x = x * 10 + 2;<br>
x = x * 10 + 3;<br>
cout&lt;&lt;x;    -&gt;    1428570123;</p>
<!-- more -->
<p>下面是题目部分：（能够在题库里找到）</p>
<p>求位数：<br>
输入 n (0 &lt;= n &lt;= 10 ** 18)<br>
输出 n 是几位数。<br>
程序：<br>
#include<iostream><br>
using namespace std;<br>
typedef long long ll;<br>
ll size(ll x){<br>
ll ans = 0;<br>
do{<br>
ans ++;<br>
x /= 10;<br>
} while (x);<br>
return ans;<br>
}<br>
int main(){<br>
ll n;<br>
cin&gt;&gt;n;<br>
cout&lt;&lt;size(n)&lt;&lt;endl;<br>
return 0;<br>
}</p>
<p>求二进制位数：（同上，只需要把x /= 10改为x /= 2即可）<br>
有多少个0（部分）<br>
typedef long long ll;<br>
ll count0(ll x){<br>
do{<br>
if(x % 10 == 0) ans ++;<br>
x /= 10;<br>
} while(x &gt; 0);<br>
return ans;<br>
}<br>
共有多少个0（上面加上……）<br>
for(ll i = 0;i &lt;= n; i++)<br>
sum += count0(i);</p>
<p>数字反转<br>
if(n &lt; 0){<br>
cout&lt;&lt;&quot;-&quot;;<br>
n = -n;<br>
}<br>
do{<br>
ans = ans * 10 + n % 10;<br>
n /= 10;<br>
} while(n);</p>
<p>bool lucky(ll x){<br>
do{<br>
ll r = x % 10;<br>
if(r != 5 &amp;&amp; r != 8)<br>
return 0;<br>
x /= 10;<br>
} while(x);<br>
return 1;<br>
}</p>
<p>幸运的58<br>
ll k, n = 5, i = 1;<br>
cin&gt;&gt;k;<br>
while(i&lt;k){<br>
n ++;<br>
if(lucky(n))<br>
i ++;<br>
}<br>
cout&lt;&lt;n&lt;&lt;&quot;endl&quot;;</p>
<p>里程表<br>
ll countk(ll x, ll k){<br>
ll cnt = 0;<br>
do{<br>
if(x % 10 == k)    cnt ++;<br>
x /= 10;<br>
} while(x);<br>
return cnt;<br>
}<br>
for(ll i = 1;i &lt;= 5; i++)<br>
if(countk(i, 3) == 0 &amp;&amp; countk(i, 8) == 0)<br>
ans ++;</p>

          </div>
        </div>

        
          <div class="next-post">
            <a class="purple-link" href="https://windowsregedit.github.io/post/hello-gridea/">
              <h3 class="post-title">
                下一篇：Hello Gridea
              </h3>
            </a>
          </div>
          
      </div>

      

      <div class="site-footer">
  <div class="slogan">温故而知新</div>
  <div class="social-container">
    
      
    
      
    
      
    
      
    
      
    
  </div>
  Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a> | <a class="rss" href="https://windowsregedit.github.io/atom.xml" target="_blank">RSS</a>
</div>


    </div>
    <script type="application/javascript">

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>




  </body>
</html>
